package org.zstacks.znet;

import java.io.IOException;
import java.util.concurrent.ExecutorService;

import org.zstacks.znet.nio.DispatcherManager;

public class ServerDispatcherManager extends DispatcherManager{ 
	protected ServerEventAdaptor serverEventAdaptor;
	
	public ServerDispatcherManager(
			ServerEventAdaptor serverEventAdaptor,
			ExecutorService executor, 
			int engineCount, 
			String engineNamePrefix) throws IOException{ 
		super(new MessageCodec(), executor, engineCount, engineNamePrefix);
		this.serverEventAdaptor = serverEventAdaptor;
	}
	
	public ServerDispatcherManager(ServerEventAdaptor serverEventAdaptor, int engineCount) throws IOException {
		this(serverEventAdaptor, DispatcherManager.newDefaultExecutor(), 
				engineCount, ServerDispatcherManager.class.getSimpleName());
	}
	
	public ServerDispatcherManager(int engineCount) throws IOException {
		this(new ServerEventAdaptor(), engineCount);
	}
	
	/**
	 * 传统nio框架采用一个Selector来支撑大量连接事件的管理和触发已经遇到瓶颈，因此现在各种nio框架的新版本都采用多个Selector并存的结构，由多个Selector均衡地去管理大量连接
	 * http://www.blogjava.net/killme2008/archive/2009/10/06/297314.html
	 * 
	 * Mina的默认open的Selector是cpu+2个。 其中一个集中处理 OP_ACCEPT事件， 其他处理 读写事件
	 * @param serverEventAdaptor
	 * @throws IOException
	 */
	public ServerDispatcherManager(ServerEventAdaptor serverEventAdaptor) throws IOException  { 
		this(serverEventAdaptor, DispatcherManager.defaultDispatcherSize()); 
	}

	public ServerDispatcherManager() throws IOException  { 
		this(DispatcherManager.defaultDispatcherSize());
	}  
	
	public ServerEventAdaptor buildEventAdaptor(){ 
		return this.serverEventAdaptor;
	}
}
